package development.socket;

import state.AlertListener;
import state.GameAlert;
import state.GameGlobal;
import state.socket.DataPackage;
import state.util.array.StringArray;
import development.Constants;
import development.Global;
import development.app.Frm18plus;
import development.app.FrmCuaHang;
import development.app.FrmDanhSachNguoiChoi;
import development.app.FrmDanhSachVatPhamQuyDoi;
import development.app.FrmDauGiaNguoc;
import development.app.FrmKetBan;
import development.app.FrmLottery;
import development.app.FrmSuKienHot;
import development.app.FrmThoiTiet;
import development.app.FrmThoiTietTinhThanh;
import development.app.FrmTinTuc;
import development.app.football.FrmBongDa;
import development.app.football.FrmBongDaBetting;
import development.app.mxh.FrmAccount;
import development.app.mxh.FrmMyAlbum;
import development.app.mxh.FrmUserGroup;
import development.app.mxh.FrmWall;
import development.bussiness.FriendBussiness;
import development.group.FrmGroupMenu;

public class AppHandle extends RequestGroupHandle implements AlertListener {
  public static boolean isOnSignOut = false;

  private FriendBussiness friendBussiness;
  public int chooseAppIndex;

  public AppHandle() {
    friendBussiness = new FriendBussiness();
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    // Khi mất kết nối với máy chủ
    if (alertId == 96 || alertId == 99) {
      GameGlobal.nextState(FrmGroupMenu.instance(), null);
    }
  }

  public void processPackage(DataPackage dataPackage) {
    super.processPackage(dataPackage);
    int header = dataPackage.getHeader();
    switch (header) {

    /** Authenticate */
    case ProtocolConstants.ResponseHeader.SALT_REPONSE:
      String salt = dataPackage.nextString();
      SocketClientUtil.authenToServerBySessionId(Global.appClient, Global.loginInfo.getUserName(), Global.session.getId(), salt);
      break;
    case ProtocolConstants.ResponseHeader.AUTHENTICATE_SUCCESS_RESPONSE:
      onAuthenSuccess();
      break;
    case ProtocolConstants.ResponseHeader.AUTHENTICATE_FAIL_RESPONSE:
      GameGlobal.alert.showAlert1(this, GameAlert.OK_TYPE, StringArray.create1( "Đăng nhập vào máy chủ ứng dụng thất bại", "Hãy thử đăng nhập lại" ), 99);
      break;

    case ProtocolConstants.ResponseHeader.FRIEND_LIST_RESPONSE:
      friendBussiness.onGetFriendList(dataPackage);
      break;

    /** Football */
    case FrmBongDaBetting.FOOTBALL_PROGRAM_PHASE_BETTING_ONLINE_RESPONSE:
    case FrmBongDaBetting.FOOTBALL_BETTING_OBJECT_ONLINE_RESPONSE:
    case FrmBongDaBetting.FOOTBALL_BETTING_SUBMIT_ONLINE_RESPONSE:
      ((FrmBongDaBetting) FrmBongDaBetting.instance()).onRecieveData(dataPackage);
      break;
    /* Danh sach nguoi choi */
    case FrmDanhSachNguoiChoi.PROGRAM_LIST_RESPONSE:
    case FrmDanhSachNguoiChoi.PHASE_LIST_RESPONSE:
    case FrmDanhSachNguoiChoi.VOTING_LIST_RESPONSE:
    case FrmDanhSachNguoiChoi.SEARCH_USER_RESPONSE:
    case FrmDanhSachNguoiChoi.VOTING_LIST_HISTORY_RESPONSE:
    case FrmDanhSachNguoiChoi.DAU_GIA_NGUOC_RESULT_RESPONSE:
      ((FrmDanhSachNguoiChoi) FrmDanhSachNguoiChoi.instance()).onRecieveData(dataPackage);
      break;
    /* Danh sach vat pham quy doi */
    case FrmDanhSachVatPhamQuyDoi.IPLAY_CONTENT_RESPONSE:
    case FrmDanhSachVatPhamQuyDoi.IPLAY_CATEGOGRY_RESPONSE:
    case FrmDanhSachVatPhamQuyDoi.IPLAY_ORDER_RESPONSE:
    case FrmDanhSachVatPhamQuyDoi.IPLAY_BILLING_LIST_RESPONSE:
      ((FrmDanhSachVatPhamQuyDoi) FrmDanhSachVatPhamQuyDoi.instance()).onRecieveData(dataPackage);
      break;
    /* Dau gia nguoc */
    case FrmDauGiaNguoc.DAU_GIA_NGUOC_PHASE_LIST_RESPONSE:
    case FrmDauGiaNguoc.DAU_GIA_NGUOC_VOTING_OBJECT_RESPONSE:
    case FrmDauGiaNguoc.DAU_GIA_NGUOC_DAT_GIA_RESPONSE:
    case FrmDauGiaNguoc.DAU_GIA_NGUOC_USERS_RESPONSE:
      ((FrmDauGiaNguoc) FrmDauGiaNguoc.instance()).onRecieveData(dataPackage);
      break;
    /* Su kien HOT */
    case FrmSuKienHot.EVENT_CATEGORY_RESPONSE:
    case FrmSuKienHot.EVENT_CONTENT_RESPONSE:
      ((FrmSuKienHot) FrmSuKienHot.instance()).onRecieveData(dataPackage);
      break;

    /* 18+ */
    case Frm18plus.CATEGOGRY_RESPONSE:
    case Frm18plus.CONTENT_CLIP_RESPONSE:
    case Frm18plus.CANCEL_RESPONSE:
    case Frm18plus.CONTENT_IMAGE_RESPONSE:
    case Frm18plus.CONTENT_STORY_RESPONSE:
    case Frm18plus.DOWNLOAD_CHARGING_RESPONSE:
      ((Frm18plus) Frm18plus.instance()).onRecieveData(dataPackage);
      break;

    /* Tin tuc */
    case FrmTinTuc.NEWS_CATEGOGRY_RESPONSE:
    case FrmTinTuc.NEWS_CONTENT_RESPONSE:
    case FrmTinTuc.CANCEL_RESPONSE:
      ((FrmTinTuc) FrmTinTuc.instance()).onRecieveData(dataPackage);
      break;

    /* Mạng XH My album, HOT album */
    case FrmMyAlbum.PERMISSION_FAIL_RESPONSE:
    case FrmMyAlbum.PERMISSION_SUCCESS_RESPONSE:
    case FrmMyAlbum.CREATED_ALBUM_RESPONSE:
    case FrmMyAlbum.CHANGE_ALBUM_NAME_RESPONSE:
    case FrmMyAlbum.DELETE_ALBUM_RESPONSE:
    case FrmMyAlbum.DELETE_PHOTO_RESPONSE:
    case FrmMyAlbum.LIST_MY_ALBUM_RESPONSE:
    case FrmMyAlbum.LIST_HOT_ALBUM_RESPONSE:
    case FrmMyAlbum.CREATE_PHOTO_RESPONSE:
    case FrmMyAlbum.LIST_PHOTO_RESPONSE:
    case FrmMyAlbum.VIEW_PHOTO_RESPONSE:
    case FrmMyAlbum.VIEW_ZOOM_PHOTO_RESPONSE:
    case FrmMyAlbum.LIKE_PHOTO_RESPONSE:
    case FrmMyAlbum.VIEW_COMMENT_RESPONSE:
    case FrmMyAlbum.LIKE_COMMENT_RESPONSE:
    case FrmMyAlbum.UP_TO_HOT_ALBUM_RESPONSE:
    case FrmMyAlbum.DELETE_COMMENT_RESPONSE:
      ((FrmMyAlbum) FrmMyAlbum.instance()).onRecieveData(dataPackage);
      break;

    /* Mạng XH Tuong */
    case FrmWall.PERMISSION_FAIL_RESPONSE:
    case FrmWall.PERMISSION_SUCCESS_RESPONSE:
    case FrmWall.LIST_COMMENT_RESPONSE:
    case FrmWall.LIKE_COMMENT_RESPONSE:
    case FrmWall.DELETE_COMMENT_RESPONSE:
    case FrmWall.COMMENT_DETAIL_RESPONSE:
      ((FrmWall) (FrmWall.instance())).onRecieveData(dataPackage);
      break;
    /* Bang hoi */
    case FrmUserGroup.LIST_USER_GROUP_RESPONSE:
    case FrmUserGroup.CHANGE_AVATAR_RESPONSE:
    case FrmUserGroup.TRANSFER_MONEY_RESPONSE:
    case FrmUserGroup.FEED_BACK_RESPONSE:
    case FrmUserGroup.LEAVE_USER_GROUP_RESPONSE:
    case FrmUserGroup.JOIN_USER_GROUP_RESPONSE:
    case FrmUserGroup.VIEW_AVATAR_RESPONSE:
    case FrmUserGroup.LIST_MEMBER_RESPONSE:
    case FrmUserGroup.EDIT_USER_GROUP_RESPONSE:
      ((FrmUserGroup) FrmUserGroup.instance()).onRecieveData(dataPackage);
      break;
    /* Tai khoan */
    case FrmAccount.ACCOUNT_INFO_RESPONSE:
      ((FrmAccount) FrmAccount.instance()).onRecieveData(dataPackage);
      break;

    // Thời tiết
    case FrmThoiTietTinhThanh.GET_THOITIET_LIST_RESPONSE:
      ((FrmThoiTiet) FrmThoiTiet.instance()).onRecieveData(dataPackage);
      break;

    // Bóng đá
    case FrmBongDa.FOOTBALL_CATEGORY_RESPONSE:
    case FrmBongDa.FOOTBALL_CONTENT_RESPONSE:
    case FrmBongDa.FOOTBALL_PROGRAM_BETTING_ONLINE_RESPONSE:
    case FrmBongDa.FOOTBALL_PROGRAM_PHASE_BETTING_ONLINE_RESPONSE:
    case FrmBongDa.FOOTBALL_BETTING_OBJECT_ONLINE_RESPONSE:
    case FrmBongDa.FOOTBALL_BETTING_SUBMIT_ONLINE_RESPONSE:
      ((FrmBongDa) FrmBongDa.instance()).onRecieveData(dataPackage);
      break;

    // Xổ số
    case FrmLottery.LOTTERY_TYPE_LIST_RESPONSE:
    case FrmLottery.LOTTERY_SCHEDULE_RESPONSE:
    case FrmLottery.LOTTERY_RESULT_RESPONSE:
    case FrmLottery.LOTTERY_BET_RESPONSE:
    case FrmLottery.LOTTERY_PHASE_RESPONSE:
    case FrmLottery.LOTTERY_BET_BRAND_LIST_RESPONSE:
    case FrmLottery.LOTTERY_BET_BRAND_RESPONSE:
      ((FrmLottery) FrmLottery.instance()).onRecieveData(dataPackage);
      break;

    // Kết bạn
    case FrmKetBan.GET_PROVINCE_LIST_RESPONSE:
    case FrmKetBan.GET_FRIEND_LIST_RESPONSE:
    case FrmKetBan.GET_PROVINCE_COUNT_RESPONSE:
    case FrmKetBan.GET_FRIEND_COUNT_RESPONSE:
    case FrmKetBan.GET_PAID_RESPONSE:
      ((FrmKetBan) FrmKetBan.instance()).onRecieveData(dataPackage);
      break;
      
      
    // Cửa hàng
    case FrmCuaHang.GET_CATEGORY_LIST_RESPONSE:
    case FrmCuaHang.GET_CONTENT_LIST_RESPONSE:
    case FrmCuaHang.GET_CATEGORY_PARENT_LIST_RESPONSE:
    case FrmCuaHang.GET_CONTENT_DATA_RESPONSE:
    case FrmCuaHang.GET_COUNT_RESPONSE:
    case FrmCuaHang.GET_DOWNLOAD_URL_RESPONSE:
    case FrmCuaHang.GET_VAS_CATEGORY_LIST_RESPONSE:
    case FrmCuaHang.GET_VAS_CONTENT_LIST_RESPONSE:
    case FrmCuaHang.GET_VAS_CATEGORY_PARENT_LIST_RESPONSE:
    case FrmCuaHang.GET_VAS_COUNT_RESPONSE:
    case FrmCuaHang.GET_DOWNLOAD_RESPONSE:
      ((FrmCuaHang) FrmCuaHang.instance()).onRecieveData(dataPackage);
      break;
    default:
      System.err.println("AppHandle : Receive unknown message header " + header);
      break;
    }
  }

  public void onConnectDone() {
    super.onConnectDone();
    SocketClientUtil.startAuthenToServer(Global.appClient, Global.loginInfo.getUserName());
  }

  public void onConnectFail() {
    super.onConnectFail();
  }

  private void onAuthenSuccess() {
    // Send the device solusion of client to server
    DataPackage deveiceSolusionPackage = DataPackage.createDataPackage1(ProtocolConstants.RequestHeader.CLIENT_DEVICE_NOTIFY_REQUEST);
    deveiceSolusionPackage.putInt(Constants.SCREEN_WIDTH);
    deveiceSolusionPackage.putInt(Constants.SCREEN_HEIGHT);
    Global.appClient.write(deveiceSolusionPackage);
  }

  public void onDisconnect() {
    super.onDisconnect();
    if (Global.session != null) {
      SocketClientUtil.connectToAppServer(Global.appService.getUrl(), Global.appService.getPort(), Global.appHandle);
    }
  }
}
